package com.crm.service.impl.messageprompt;

import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.customMapper.messageprompt.MessagePromptCustomMapper;
import com.crm.dao.mapper.messageprompt.MessagePromptMapper;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.messageprompt.MessagePrompt;
import com.crm.model.entity.messageprompt.MessagePromptExample;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.messageprompt.MessagePromptVO;
import com.crm.service.messageprompt.MessagePromptDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @Description: 消息提示
 * @Author lixq-f
 * @Date 2021/5/18
 * @Version V1.0
 **/
@Service
public class MessagePromptDbServiceImpl implements MessagePromptDbService {

    @Autowired
    private MessagePromptMapper messagePromptMapper;
    @Autowired
    private MessagePromptCustomMapper promptCustomMapper;


    /**
     * @Description: 获取消息提示列表
     * @Param: []
     * @Return: java.util.List<com.crm.model.entity.messageprompt.MessagePrompt>
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public List<MessagePromptVO> queryMessagePromptList(String accountId, Date startTm, Date endTm) {
        return promptCustomMapper.queryMessagePromptList(accountId, startTm, endTm);
    }


    /**
     * @Description: 更新消息状态为已读
     * @Param: [messagePrompt]
     * @Return: java.lang.Boolean
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public Boolean updateMessageState(MessagePrompt messagePrompt) {
        return messagePromptMapper.updateByPrimaryKeySelective(messagePrompt) > 0;
    }


    /**
     * @Description: 未读消息数
     * @Param: []
     * @Return: java.lang.Integer
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public Integer unreadMessageCount(String accountId, Date startTm, Date endTm) {
        MessagePromptExample example = new MessagePromptExample();
        MessagePromptExample.Criteria criteria = example.createCriteria();
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andMessageStateEqualTo((short)0);
        criteria.andDeleteStatusEqualTo((short)0);
        criteria.andCreateTmBetween(startTm,endTm);
        return (int)messagePromptMapper.countByExample(example);
    }

    /**
     * 插入消息
     * @param messagePrompt
     */
    @Override
    public void insertMessagePrompt(MessagePrompt messagePrompt) {
        messagePromptMapper.insertSelective(messagePrompt);
    }

    /**
     * 根据类型插入消息
     * @param contract 合同
     * @param type 类型 1回款 2退款
     */
    @Override
    public void insertMsg(CustomerContract contract, Integer type) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        //全部完成插入一条消息
        MessagePrompt messagePrompt = new MessagePrompt();
        messagePrompt.setMessageId(CommonUtils.getGUID());
        messagePrompt.setMessageType((Short.valueOf("4")));
        if (type == 1){
            messagePrompt.setMessageContent("您的合同编号为："+contract.getContractNumber()+"已回款完成");
        }
        if (type == 2){
            messagePrompt.setMessageContent("您的合同编号为："+contract.getContractNumber()+"已成功退款");
        }
        messagePrompt.setCreatePeopleId(userAccount.getAccountId());
        messagePrompt.setCreateTm(new Date());
        insertMessagePrompt(messagePrompt);
    }


}
